//
//  Copyright (C) 2010 Vadim Ushakov
// 
//  This program is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License as published by the Free
//  Software Foundation; either version 2 of the License, or (at your option)
//  any later version.
// 
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
//  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
//  for more details.
//  
//  You should have received a copy of the GNU General Public License along
//  with this program; if not, write to the Free Software Foundation, Inc.,
//  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include "kernelSpinLock.h"
#include "kernelMisc.h"
#include "core1/kernelMultitasker.h"
#include "kernelPerformanceCounter.h"
#include "kernelProcessorLevel.h"
#include "mach/interlocked.h"
#include "kernelShutdown.h"
#include "kernelSysTimer.h"
#include "kernelError.h"
#include <string.h>

static int pcSpinLocksAcquired = -1;

#define kernelGetCpuId() 1

int kernelSpinLockRelease(volatile kernelSpinLock* lck, int oldLevel)
{
	int status = 0;
	int id;

	// Make sure the pointer we were given is not NULL
	if (lck == NULL)
	{
		//FIXME: panic here
		return (status = ERR_NULLPARAMETER);
	}

	int currentLevel = kernelGetProcessorLevel();

	if (currentLevel < oldLevel || currentLevel != lck->level)
	{
		//FIXME: panic here
		return (status = ERR_INVALID);
	}

	// Get the process Id of the current process
	id = kernelGetCpuId();

	// Make sure it's a valid process Id
	if (id <= 0)
	{
		//FIXME: panic here
		return (id);
	}

	// Make sure that the current lock really belongs to this process.

	if (lck->id != id)
	{
		//FIXME: panic here
		return (status = ERR_NOLOCK);
	}

	// Recursive lock
	if (lck->recursive > 1)
	{
		if (currentLevel != oldLevel)
		{
			kernelPanic("currentLevel = %d, oldLevel = %d", currentLevel, oldLevel);
			//FIXME: panic here
			//return (status = ERR_INVALID);
		}
		lck->recursive--;
	}
	else
	{
		lck->id = 0;
		if (oldLevel < currentLevel)
			kernelLowerProcessorLevel(oldLevel);
	}
	return (status = 0);
}


int kernelSpinLockAcquire(volatile kernelSpinLock* lck)
{
	int status = 0;
	int id;

	// Make sure the pointer we were given is not NULL
	if (lck == NULL)
		return (status = ERR_NULLPARAMETER);

	int level = kernelGetProcessorLevel();

	if (lck->level < level || lck->level < kernelProcessorLevelDispatch)
		return (status = ERR_INVALID);

	// Get the process Id of the current process
	id = kernelGetCpuId();
	if (id <= 0)
		return (id);

	// Check whether the processor already has the lock.
	if (lck->id == id)
	{
		// If the lock is recursive, acquire it recursively
		if (lck->recursive)
		{
			if (lck->level != level)
			{
				//FIXME: panic here
				return (status = ERR_INVALID);
			}
			lck->recursive++;
			return (status = level);
		}
		return (status = ERR_ALREADY);
	}

	if (level < lck->level)
		kernelRaiseProcessorLevel(lck->level);

	status = machWaitForInterlockedCompareExchange(&lck->id, id, 0);

	if (status != 0)
	{
		if (level > lck->level)
			kernelLowerProcessorLevel(level);
		return (status = ERR_BUG);
	}

	kernelPerformanceCounterInc1(pcSpinLocksAcquired);

	return (status = level);
}


int kernelSpinLockSetRecursive(volatile kernelSpinLock *lck)
{
	int status;

	if (lck == NULL)
		return (status = ERR_NULLPARAMETER);

	if (!lck->recursive)
		lck->recursive = 1;

	return (status = 0);
}

int kernelSpinLockSetProcessorLevel(volatile kernelSpinLock *lck, int level)
{
	int status;

	if (lck == NULL)
		return (status = ERR_NULLPARAMETER);

	if (level < kernelProcessorLevelDispatch)
		return (status = ERR_INVALID);

	lck->level = level;

	return (status = 0);
}

int kernelInitializeSpinLock(void)
{
	pcSpinLocksAcquired = kernelPerformanceCounterRegisterStatic("kernel.spinlocks_acquired");
	return (0);
}

